package com.bawei.monthdemo2207a.month01.game

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.TextView
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.FabPosition
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.ayvytr.adapter.smart
import com.bawei.monthdemo2207a.R
import com.blankj.utilcode.util.ToastUtils
import java.io.File

private val dir = "/sdcard/Download"

/**
 * 作者：赵亮
 * 时间：2024/12/26 16:05
 * 功能：游戏页面
 */
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun GamePage() {

    val context = LocalContext.current

    // RecycleView 适配器
    val adapter = context.smart(
        mutableListOf<File>(),
        R.layout.item_file,
        { item, position ->
            findViewById<ImageView>(R.id.iv_image).load(R.drawable.logo)
            findViewById<TextView>(R.id.tv_name).text = item.name
        }) {}


    LaunchedEffect(key1 = "") {
        adapter.update(File(dir).listFiles().toList())
    }

    var isShowAdd by remember {
        mutableStateOf(false)
    }

    // 是否显示新建文件夹对话
    var isShowNewFileDirDialog by remember {
        mutableStateOf(false)
    }

    // 新建文件夹的名字
    var fileDirName by remember {
        mutableStateOf("新建文件夹")
    }


    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 24.dp), horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "文档管理", fontSize = 22.sp)

        Box {
            AndroidView(modifier = Modifier.fillMaxSize(), factory = {
                val recyclerView = RecyclerView(it)
                recyclerView.layoutManager = LinearLayoutManager(it)
                recyclerView.adapter = adapter
                // 列表条目长按
                adapter.itemLongClickListener = { file: File, i: Int ->
                    adapter.remove(file)
                    file.delete()
                    ToastUtils.showShort("删除成功")
                }

                // 条目点击
                adapter.itemClickListener = { file: File, i: Int ->
                    ToastUtils.showShort("点击了${file.name}${file.absolutePath}")
                }

                recyclerView
            })
            Icon(
                imageVector = Icons.Default.Add,
                contentDescription = null,
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .padding(bottom = 100.dp)
                    .clickable {
                        isShowAdd = true
                    }
            )
            // 新建页面
            if (isShowAdd) {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(color = Color.White)
                ) {
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .align(Alignment.BottomCenter)
                            .padding(bottom = 180.dp),
                        horizontalArrangement = Arrangement.SpaceAround
                    ) {
                        Column(horizontalAlignment = Alignment.CenterHorizontally) {
                            Image(
                                painter = painterResource(id = R.drawable.logo_weixin),
                                contentDescription = null,
                                modifier = Modifier
                                    .clip(
                                        CircleShape
                                    )
                                    .size(80.dp)
                            )
                            Text(text = "照片和视频")
                        }
                        Column(horizontalAlignment = Alignment.CenterHorizontally) {
                            Image(
                                painter = painterResource(id = R.drawable.logo_weixin),
                                contentDescription = null,
                                modifier = Modifier
                                    .clip(
                                        CircleShape
                                    )
                                    .size(80.dp)
                            )
                            Text(text = "文件和文间夹")
                        }
                        Column(
                            horizontalAlignment = Alignment.CenterHorizontally,
                            modifier = Modifier.clickable {
                                isShowNewFileDirDialog = true
                            }) {
                            Image(
                                painter = painterResource(id = R.drawable.logo_weixin),
                                contentDescription = null,
                                modifier = Modifier
                                    .clip(
                                        CircleShape
                                    )
                                    .size(80.dp)
                            )
                            Text(text = "新建文件夹")
                        }
                        if (isShowNewFileDirDialog) {
                            AlertDialog(
                                onDismissRequest = { },
                                text = {
                                    OutlinedTextField(value = fileDirName, onValueChange = {
                                        fileDirName = it
                                    })
                                },
                                dismissButton = {
                                    Text(text = "取消", modifier = Modifier.clickable {
                                        isShowNewFileDirDialog = false
                                    })
                                },
                                confirmButton = {
                                    Text(text = "确定", modifier = Modifier.clickable {
                                        // 创建文件夹
                                        val file = File(dir, fileDirName)
                                        if (file.exists()) {
                                            ToastUtils.showShort("文件夹已存在")
                                        } else {
                                            file.mkdir()
                                            ToastUtils.showShort("创建成功")
                                            isShowNewFileDirDialog = false
                                            isShowAdd = false
                                        }
                                    })
                                })
                        }
                    }
                    Icon(
                        imageVector = Icons.Default.Clear,
                        contentDescription = null,
                        modifier = Modifier
                            .align(Alignment.BottomCenter)
                            .padding(bottom = 100.dp)
                            .clickable { isShowAdd = false }
                    )
                }
            }
        }

    }
}

@Preview
@Composable
fun GamePagePreview() {
    GamePage()
}

